<?php


namespace app\shopConsole\controller;


use app\BaseController;

use app\console\validate\TagValidate;
use app\lib\exception\ParamException;
use app\model\ShopFreight;
use app\model\ShopFreightSpecial;
use app\Request;
use app\shopConsole\service\freight\FreightQueryService;
use app\shopConsole\service\freight\FreightService;
use app\shopConsole\service\freight\FreightSpecialService;
use app\shopConsole\validate\FerightSpecialValidate;
use app\shopConsole\validate\FerightValidate;
use think\App;
use think\exception\ValidateException;

//运费模板
function freight2dict($list)
{
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'name' => $list['name'],
        'shop_id' => $list['shop_id'],
        'type' => $list['type'],
        'min_amount' => $list['min_amount'],
        'per_amount' => $list['per_amount'],
        'min_unit' => $list['min_unit'],
        'per_unit' => $list['per_unit'],
        'status' => $list['status'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
        'special' => '',
    ];
}

//特殊模板
function special2dict($list)
{
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'freight_id' => $list['freight_id'],
        'min_amount' => $list['min_amount'],
        'per_amount' => $list['per_amount'],
        'min_unit' => $list['min_unit'],
        'per_unit' => $list['per_unit'],
        'province' => $list['province'],
        'city' => $list['city'],
        'district' => $list['district'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
    ];
}

//运费模板加特殊模板列表
function freightSpecial2dict($freight)
{
    $data = freight2dict($freight);
    if (is_object($freight['special']))
    {
        $data['special'] = array_map(fn($list) => special2dict($list),$freight['special']->toArray());
    }else{
        $data['special'] = array_map(fn($list) => special2dict($list),$freight['special']);
    }

    return $data;
}

//运费
class FreightController extends BaseController
{

    //添加运费
    public function createFreight(Request $request, FreightService $freightService)
    {
        $param = $request->param(['name', 'type', 'min_amount', 'per_amount', 'min_unit', 'per_unit']);
        $param['special'] = $request->param('special_regions', '');
        $param['shop_id'] = $request->id;
        try
        {
            validate(FerightValidate::class)
                ->scene('create')
                ->check($param);

        } catch (ValidateException $e) {
            throw new ParamException($e->getMessage());
        }

        $freight = $freightService->create($param);
        return $this->returnJson(freightSpecial2dict($freight));
    }

    //添加特殊运费
    public function createFreightSpecial(Request $request, FreightSpecialService $freightSpecialService, ShopFreight $shopFreight)
    {
        $param = $request->param(['min_amount', 'per_amount', 'per_unit', 'min_unit', 'province', 'city', 'district']);
        $param['freight_id'] = $shopFreight['id'];

        validate(FerightSpecialValidate::class)
            ->scene('create')
            ->check($param);

        $special = $freightSpecialService->create($param);

        return $this->returnJson(special2dict($special));
    }

    //获取运费列表
    public function get(Request $request, FreightQueryService $freightQueryService)
    {
        $all = $request->get('all', false);
        $page = $request->get('page', 1);
        $perPage = $request->get('perPage', config('app.allow_limit'));

        if (!$all)
        {
            validate(FerightValidate::class)
                ->scene('get')
                ->check([
                    'page'         => $page ,
                    'perPage'      => $perPage ,
                ]);

            $freights = $freightQueryService->getPageListByShopId($request->id, $page ,$perPage);
        }else{
            $freights = $freightQueryService->getListByShopId($request->id);
        }

        return $this->returnList(
            array_map(fn($list) => freightSpecial2dict($list), $freights['list']->toArray()),
            $freights['pagination']['page'], $freights['pagination']['perPage'], $freights['pagination']['total']
        );

    }

    //修改运费
    public function updateFreight(Request $request, FreightService $freightService)
    {
        $param = $request->param(['name', 'type', 'min_amount', 'per_amount', 'min_unit', 'per_unit'],'');
        $param['special'] = $request->param('special_regions', '');

        validate(FerightValidate::class)
            ->scene('update')
            ->check($param);

        validate(FerightSpecialValidate::class)
            ->scene('update')
            ->check($param['special']);

        $freight = $freightService->update($param);

        return $this->returnJson(freightSpecial2dict($freight));
    }

    //修改特殊运费
    public function updateFreightSpecia(Request $request, FreightSpecialService $freightSpecialService)
    {
        $param = $request->param(['min_amount', 'per_amount', 'per_unit', 'min_unit', 'province', 'city', 'district'], '');

        if (empty($param))
        {
            throw new ParamException('未修改任何数据');
        }

        $freight = $freightSpecialService->update($param);

        return $this->returnJson(special2dict($freight));
    }

    //禁用or恢复运费
    public function freightBanOrUndo(Request $request, FreightService $freightService)
    {
        $status = $request->param('status', '');

        if ($status == 0)
        {
            $freight = $freightService->freightUndo();
        }else{
            $freight = $freightService->freightBan();
        }
        return $this->returnJson(freightSpecial2dict($freight));
    }

    //删除运费
    public function deleteFreight(FreightService $freightService)
    {
        $freightService->delete();
        return $this->returnJson();
    }

    //删除特殊运费
    public function deleteFreightSpecia(FreightSpecialService $freightSpecialService)
    {
        $freightSpecialService->delete();
        return $this->returnJson();
    }

    //获取指定运费
    public function getById(FreightQueryService $freightQueryService, ShopFreight $shopFreight)
    {
        $freight = $freightQueryService->getById($shopFreight->id);
        return $this->returnJson(freightSpecial2dict($freight));
    }

    //获取指定特殊运费
    public function getFreightSpecialById(ShopFreightSpecial $shopFreightSpecial)
    {
        return $this->returnJson(special2dict($shopFreightSpecial));
    }

}